package com.sky.demo.utils;

import java.util.Stack;

import android.app.Activity;

/** 
* @ClassName: ActivityManager 
* @Description: TODO activity管理
* @author sky
* @date 2015年4月12日 下午1:33:47 
*  
*/
public class ActivityManager {
    private static Stack<Activity> activityStack;
    private static ActivityManager instance;
    private ActivityManager() {
    }
    public static ActivityManager getScreenManager() {
        if (instance == null) {
            instance = new ActivityManager();
        }
        return instance;
    }
    /**
     * @param activity 退出栈顶Activity
     */
    public void popActivity(Activity activity) {
        if (activity != null) {
           //在从自定义集合中取出当前Activity时，也进行了Activity的关闭操作
            activity.finish();
            activityStack.remove(activity);
            activity = null;
        }
    }
    /**
     * @return 获得当前栈顶Activity
     */
    public Activity currentActivity() {
        Activity activity = null;
       if(!activityStack.empty())
         activity= activityStack.lastElement();
        return activity;
    }
    /**
     * @param activity 将当前Activity推入栈中
     */
    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }
    /**
     * @param cls 退出栈中所有Activity 
     */
    public void popAllActivityExceptOne(Class cls) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                break;
            }
            popActivity(activity);
        }
    }
} 